package com.soft.sitegenerator.business.hibernate;

import java.util.Collection;
import java.util.Date;

import com.soft.sitegenerator.dao.HibernateDAO;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;

public class Post {

	private Long id;

	private User user;

	private NewsGroup newsGroup;

	private String text;

	private String title;

	private Date creationDate;

	private HibernateDAO hibernateDAOHib;

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public NewsGroup getNewsGroup() {
		return newsGroup;
	}

	public void setNewsGroup(NewsGroup newsGroup) {
		this.newsGroup = newsGroup;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	// HIBERNATE DAO
	public HibernateDAO getHibernateDAOHib() {
		return hibernateDAOHib;
	}

	public void setHibernateDAOHib(HibernateDAO hibernateDAOHib) {
		this.hibernateDAOHib = hibernateDAOHib;
	}

	public void load() throws ObjectNotFoundException {
		hibernateDAOHib.load(this, this.id);
	}

	public void save() {
		hibernateDAOHib.save(this);
	}

	public void update() {
		hibernateDAOHib.update(this);
	}

	public void delete() {
		hibernateDAOHib.delete(this);
	}

	public boolean exist() {
		try {
			hibernateDAOHib.exist(this, this.id);
		} catch (ObjectNotFoundException e) {
			return false;
		}
		return true;
	}

	public Collection findAll() {
		return hibernateDAOHib.find("from Post order by creationDate");
	}
}
